define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template', 'echarts', 'echarts-theme'], function ($, undefined, Backend, Table, Form, Template, Echarts) {

    var Controller = {
        index: function () {
            $(".btn-add").data('area',["80%",'80%']);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/goods/index' + location.search,
                    add_url: 'goods/goods/add',
                    edit_url: 'goods/goods/edit',
                    del_url: 'goods/goods/del',
                    multi_url: 'goods/goods/multi',
                    table: 'goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                // searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),class:'gid'},
                        {field: 'number', title: __('Number'),sortable:true},
                        {field: 'title', title: __('Title'),operate: 'LIKE',class: 'title',cellStyle:function (value, row, index) {
                                return {
                                    css: {
                                        "min-width": "100px",
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width": "120px"
                                    }
                                }
                            }},
                        {field: 'specs', title: __('Specs'),class: 'specs',cellStyle:function (value, row, index) {
                                return {
                                    css: {
                                        "min-width": "100px",
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width": "120px"
                                    }
                                }
                            }},
                        // {field: 'place_origin', title: __('Place_origin')},

                        // {field: 'unit', title: __('Unit')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},

                        // {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        // {field: 'new_cost_price', title: __('Cost'), operate:'BETWEEN'},


                        {field: 'stock', title: __('Stock')},
                        {field: 'cate.name', title: __('Cate.name')},
                        {field: 'brands.name', title: __('Brands.name')},
                        {field: 'supplier.nickname', title: __('Supplier.nickname')},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        // {
                        //     field: 'sync_status',
                        //     title: __('sync_status'),
                        //     table: table,
                        //     formatter: Controller.api.formatter.operate,
                        //     operate: false,
                        //     align: 'right'
                        // },
                        {field: 'status', title: __('上下架'),  searchList: {"normal":__('上架'),"hidden":__('下架')},formatter: Controller.api.formatter.custom},
                        // {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        //自定义栏位,custom是不存在的字段
                        // {
                        //     field: 'retention',
                        //     title: __('滞留时间'),
                        //     operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime
                        //     // searchList: Config.hslist,
                        //     // operate: false,
                        // },
                        // {field: 'custom', title: __('监控'), operate: false, formatter: Controller.api.formatter.custom},
                        {field: 'operate', title: __('Operate'), table: table, buttons: [
                                {
                                    name: 'addpgoods',
                                    title: __('采购'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-shopping-basket',
                                    hidden:function(row){
                                        // if(row.status != '审核'){
                                        //     return true;
                                        // }
                                    },
                                    click: function(data){
                                        var gid = $(this).parents('tr').find('.gid').html()
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['50%', '90%'],
                                            title: "采购商品",
                                            content: 'goods/addpgoods?ids='+gid,
                                            // content: Config.this_url+'/purchase/requisition',
                                        })
                                    }},
                                {
                                    name: 'stocklist',
                                    title: __('库存明细'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-university',
                                    hidden:function(row){
                                        // if(row.status != '审核'){
                                        //     return true;
                                        // }
                                    },
                                    click: function(data){
                                        var gid = $(this).parents('tr').find('.gid').html()
                                        var purl = 'goods/stocklist?ids='+gid;
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['80%', '90%'],
                                            title: "库存明细",
                                            content: purl,
                                            // content: Config.this_url+'/goods/lists?ids='+gid,
                                        })
                                    }},
                                {
                                    name: 'lists',
                                    title: __('物料组成'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-cubes',
                                    hidden:function(row){
                                        if(row.is_material == 1){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var gid = $(this).parents('tr').find('.gid').html()
                                        var purl = 'goods/compose?ids='+gid;
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['50%', '90%'],
                                            title: "物料组成",
                                            content: purl,
                                            // content: Config.this_url+'/goods/lists?ids='+gid,
                                        })
                                    }},
                                {
                                    name: 'lists',
                                    title: __('采购记录'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-chevron-down',
                                    hidden:function(row){
                                        // if(row.status != '审核'){
                                        //     return true;
                                        // }
                                    },
                                    click: function(data){
                                        var gid = $(this).parents('tr').find('.gid').html()
                                        var purl = 'goods/lists?ids='+gid;
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['50%', '90%'],
                                            title: "采购记录",
                                            content: purl,
                                            // content: Config.this_url+'/goods/lists?ids='+gid,
                                        })
                                    }},
                                {
                                    name: 'plists',
                                    title: __('销售记录'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-chevron-up',
                                    hidden:function(row){
                                        // if(row.status != '审核'){
                                        //     return true;
                                        // }
                                    },
                                    click: function(data){
                                        var gid = $(this).parents('tr').find('.gid').html()
                                        var surl = Config.this_url+'/goods/goods/plists?ids='+gid;
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['50%', '90%'],
                                            title: "销售记录",
                                            content: surl,
                                            // content: Config.this_url+'/goods/lists?ids='+gid,
                                        })
                                    }}
                            ], events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            $('body').on('mouseenter','.title',function () {
                if (this.offsetWidth < this.scrollWidth) {
                    var that = this;
                    var text = $(this).text();
                    layer.tips(text, that,{
                        tips: 1,
                        time: 2000         //设置显示时间
                    });
                }
            })
            $('body').on('mouseenter','.specs',function () {
                if (this.offsetWidth < this.scrollWidth) {
                    var that = this;
                    var text = $(this).text();
                    layer.tips(text, that,{
                        tips: 1,
                        time: 2000         //设置显示时间
                    });
                }
            })
            $('.btn-monitor').click(function () {
                var idsarr = [];
                $('tbody').find('tr').each(function () {
                    if($(this).find('input').is(':checked')){
                        idsarr.push($(this).find('.gid').html())
                    }
                })
                if(idsarr.length > 0){
                    console.log(idsarr)
                }else{
                    layer.msg('未选中商品')
                }
                var url = Config.this_url+'/count/monitor/add?ids='+idsarr.join('-')
                // console.log(url)
                Layer.open({
                    type: 2,
                    title: __('设置商品预警'),
                    area: ["600px", "40%"],
                    content: url,
                });
            })








            $('.btn-stockgoods').click(function(){
                var url = Config.this_url+'/goods/goods/stockgoods'
                console.log(url)
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['80%', '90%'],
                    title: "商品库存状况",
                    content: url,
                    // content: Config.this_url+'/purchase/requisition',
                })
            })
            $('.btn-editlog').click(function(){
                var url = Config.this_url+'/goods/goods/editlog'
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['80%', '90%'],
                    title: "商品库存修改记录",
                    content: url,
                    // content: Config.this_url+'/purchase/requisition',
                })
            })
            $('.btn-purchasegoods').click(function(){
                var url = Config.this_url+'/goods/goods/purchasegoods'
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['80%', '90%'],
                    title: "商品采购状况",
                    content: url,
                    // content: Config.this_url+'/purchase/requisition',
                })
            })


            // 点击安装
            $(document).on("click", ".btn-install", function () {
                var that = this;
                var version = $(this).data("version");
                var goods_id = $(this).data("goods_id");
                // console.log(version)
                // console.log(goods_id)
                var url = Config.this_url+'/goods/goods/editstatus'
                $.post(url,{ids:goods_id,version:version},function (res) {
                    layer.msg(res.msg)
                    if(res.code == 1){
                        $('.btn-refresh').click()
                    }
                })

            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        stockgoods: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/goods/stockgoods' + location.search,
                }
            });
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                // searchFormTemplate: 'customformtpl',
                // commonSearch: false,

                // visible: false,
                // showToggle: false,
                // showColumns: true,
                // search:false,
                // pageSize: 10,
                // pageList:[10,25,50,100],
                showExport: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID'),class:'goods_id'},
                        {field: 'title', title: __('Title'),operate: 'LIKE'},
                        {field: 'number', title: __('Number'),class:'number'},
                        {field: 'unit', title: __('Unit'),operate:false},
                        {field: 'specs', title: __('Specs'),operate:false},
                        {field: 'name', title: __('品牌'),operate:false},
                        {field: 'cumulative', title: __('累计数量'),operate:false},
                        {field: 'new_cost', title: __('最新进价'),operate:false},
                        {field: 'weighting_cost', title: __('平均成本'),operate:false},
                        {field: 'balance', title: __('库存金额'),operate:false},
                        {field: 'stock', title: __('库存'),operate:false},
                        {field: 'store_count', title: __('商城库存'),operate:false},
                        {field: 'operate', title: __('Operate'), table: table, buttons: [

                            ], events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            $('table').on('dblclick','td',function(){
                var goods_id = $(this).parents('tr').find('.goods_id').html()
                var url = Config.this_url+'/goods/goods/seachbill?goods_id='+goods_id+'&storehouse_id='+$('#storehouse_id').val()
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['500px', '350px'],
                    title: "详细账单",
                    content: url,
                    // content: Config.this_url+'/purchase/requisition',
                })
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        editlog: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/goods/editlog' + location.search,
                }
            });
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                // searchFormTemplate: 'customformtpl',
                // commonSearch: false,

                // visible: false,
                // showToggle: false,
                // showColumns: true,
                // search:false,
                // pageSize: 10,
                // pageList:[10,25,50,100],
                showExport: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID'),class:'goods_id',operate:false},
                        {field: 'title', title: __('Title'),operate: 'LIKE'},
                        {field: 'number', title: __('Number'),class:'number'},
                        // {field: 'unit', title: __('Unit'),operate:false},
                        {field: 'specs', title: __('Specs'),operate:false},
                        {field: 'name', title: __('仓库名称'),operate:false},
                        {field: 'createtime', title: __('修改时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'before_num', title: __('修改前数量'),operate:false},
                        {field: 'now_num', title: __('修改后数量'),operate:false},
                        {field: 'before_balance', title: __('修改前价值'),operate:false},
                        {field: 'now_balance', title: __('修改后价值'),operate:false},
                        // {field: 'weighting_cost', title: __('平均成本'),operate:false},
                        // {field: 'balance', title: __('库存金额'),operate:false},
                        // {field: 'stock', title: __('库存'),operate:false},
                        {field: 'operate', title: __('Operate'), table: table, buttons: [

                            ], events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            $('table').on('dblclick','td',function(){
                var goods_id = $(this).parents('tr').find('.goods_id').html()
                var url = Config.this_url+'/goods/goods/seachbill?goods_id='+goods_id+'&storehouse_id='+$('#storehouse_id').val()
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['500px', '350px'],
                    title: "详细账单",
                    content: url,
                    // content: Config.this_url+'/purchase/requisition',
                })
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        purchasegoods: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/goods/purchasegoods' + location.search,
                }
            });
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                // searchFormTemplate: 'customformtpl',
                // commonSearch: false,

                // visible: false,
                // showToggle: false,
                // showColumns: true,
                // search:false,
                // pageSize: 10,
                // pageList:[10,25,50,100],
                showExport: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID'),class:'goods_id'},
                        {field: 'title', title: __('Title'),operate: 'LIKE'},
                        {field: 'number', title: __('Number'),class:'number'},
                        // {field: 'unit', title: __('Unit'),operate:false},
                        {field: 'specs', title: __('Specs'),operate:false},
                        {field: 'name', title: __('品牌'),operate:false},
                        {field: 'cumulative', title: __('累计数量'),operate:false},
                        {field: 'new_cost', title: __('最新进价'),operate:false},
                        {field: 'weighting_cost', title: __('平均成本'),operate:false},
                        {field: 'balance', title: __('库存金额'),operate:false},
                        {field: 'stock', title: __('库存'),operate:false},
                        {field: 'store_count', title: __('商城库存'),operate:false},
                        {field: 'operate', title: __('Operate'), table: table, buttons: [
                                {
                                    name: 'examine',
                                    title: __('同步'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-exchange',
                                    hidden:function(value, row, index){

                                    },
                                    click: function(data){
                                        // var goods_id = $(this).parents('tr').find('.goods_id').html()
                                        //prompt层
                                        // layer.confirm('确定同步商城库存吗？', {
                                        //     btn: ['确定','再想想'] //按钮
                                        // }, function(index){
                                        //     var url = Config.this_url +'/goods/goods/syncstock'
                                        //     $.post(url,{ids:goods_id,hid:$('#storehouse_id').val()},function (res) {
                                        //         layer.close(index);
                                        //         layer.msg(res.msg)
                                        //     })
                                        // }, function(){
                                        //
                                        // });
                                    }},
                            ], events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            $('table').on('dblclick','td',function(){
                var goods_id = $(this).parents('tr').find('.goods_id').html()
                var url = Config.this_url+'/goods/goods/seachbill?goods_id='+goods_id+'&storehouse_id='+$('#storehouse_id').val()
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['500px', '350px'],
                    title: "详细账单",
                    content: url,
                    // content: Config.this_url+'/purchase/requisition',
                })
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        dilllist: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/goods/dilllist' + location.search,
                }
            });
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                // searchFormTemplate: 'customformtpl',
                // commonSearch: false,
                visible: false,
                showToggle: false,
                showColumns: false,
                search:false,
                showExport: false,
                rowStyle:function(row, index){
                    if (row.is_cancel == 1) {
                        return{
                            css:{
                                color:'red'
                            }
                        }
                    }else{
                        return{}
                    }
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID'),class:'goods_id',formatter:function(value, row, index){
                                con = row.controller+'/detail?ids='+row.type_id
                                return '<div style="text-align: center; vertical-align: middle; " class="gid" data-id="'+con+'">'+row.id+'</div>';
                            }},
                        {field: 'createtime', title: __('创建时间'),operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'number', title: __('Number')},
                        {field: 'type', title: __('单据类型'),operate:false},
                        {field: 'hname', title: __('操作仓库'),operate:false},
                        {field: 'nickname', title: __('单据对象'),operate:false},
                        {field: 'billing', title: __('开单人'),operate:false},
                        {field: 'enter_num', title: __('入库数量'),operate:false},
                        {field: 'out_num', title: __('出库数量'),operate:false},
                        // {field: 'out_num', title: __('出库数量'),operate:false},
                        {field: 'before_num', title: __('操作前数量'),operate:false},
                        {field: 'now_num', title: __('当前库存'),operate:false},
                        {field: 'final_cost', title: __('加权成本价'),operate:false},
                        {field: 'bill_cost', title: __('单据成本'),operate:false},
                        {field: 'balance', title: __('库存金额'),operate:false},
                        {field: 'remark', title: __('备注'),operate:false},
                        {field: 'operator', title: __('操作人'),operate:false},
                        {field: 'company', title: __('部门'),operate:false},
                        {field: 'operate', title: __('Operate'), table: table, buttons: [
                            ], events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            $('table').on('dblclick','td',function(){
                var gid = $(this).parents('tr').find('.gid').data('id')
                var url = Config.this_url+'/'+gid
                    layer.open({
                        type: 2, // 1-在content中写html代码， 2-在content中写src路径
                        area: ['80%', '90%'],
                        title: "详情",
                        content: url,
                    })
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            var moren = '<option value="0">请选择</option>\n'
            $('#choose1').change(function () {
                cid =$(this).val()
                if($(this).val()>0){
                    var url = Config.this_url+'/goods/goods/getcate'
                    $.post(url,{cid:cid},function (res) {
                        var str = moren
                        if(res.data.length >0){
                            data = res.data

                            for (var i=0;i<data.length;i++){
                                str +='<option value="'+data[i].id+'">'+data[i].name+'</option>';
                            }

                        }
                        $('#choose2').find('option').remove()
                        $('#choose3').find('option').remove()
                        $('#choose2').append(str)
                        $('#choose3').append(moren)
                    })
                }
            })
            $('#choose2').change(function () {
                cid =$(this).val()
                if($(this).val()>0){
                    var url = Config.this_url+'/goods/goods/getcate'
                    $.post(url,{cid:cid},function (res) {
                        var str = moren
                        if(res.data.length >0){
                            data = res.data
                            for (var i=0;i<data.length;i++){
                                str +='<option value="'+data[i].id+'">'+data[i].name+'</option>';
                            }
                        }
                        $('#choose3').find('option').remove()
                        $('#choose3').append(str)
                    })
                }
            })

            $('.search').click(function () {
                var cate1 = $('#choose1').val()
                var cate2 = $('#choose2').val()
                var cate_id = $('#choose3').val()
                var title = $('#c-title').val()
                if(title == '' || cate_id <1 ){
                    layer.msg('参数不足')
                    // return false
                }
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['80%', '80%'],
                    title: "选择商品",
                    content: 'seach?title='+title+'&cate_id='+cate_id,
                })
            })





            $('#c-supplier_id_text').change

            var supplier_id = 0;
            $(document).on("change", "#c-supplier_id_text", function () {
                var cid = $('#c-supplier_id').val()

                if(cid == ''){
                    return  false;
                }
                if(cid == supplier_id){
                    return false;
                }
                supplier_id = cid;
                var url = Config.this_url+'/supplier/brands?ids='+cid
                // $.post(url,{},function (res) {
                //     $('#default_mobile').val(res.mobile)
                //     $('#default_address').val(res.address)
                //     $('#default_contacts').val(res.nickname)
                // })
                $.post(url,{
                    //搜索条件，上一个selectpage选择完后传过来的id作为此次搜索的条件
                    custom: {supplier_id: $('#c-supplier_id').val()},
                    keyField:'id',
                    showField:'name',
                },function(result){

                    var str = '';
                    for (var i=0;i<result.list.length;i++){
                        str +='<option value="'+result.list[i]['id']+'">'+result.list[i]['name']+'</option>'
                    }
                    $('#brands_id').find('option').remove()
                    $('#brands_id').append(str)
                    //刷新数据的关键，我也是看了文档才知道有这么一个方法，动态更改数据源
                    // $('#c-bands_id').selectPageData(result.list);
                });
            })


            Controller.api.bindevent();
        },

        addpgoods: function () {
            Controller.api.bindevent();
        },
        addsalelog: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        compose: function () {
            $(".btn-add").click(function () {
                if($('#c-storehouse_id').val()== 0 ){
                    layer.msg('未选中出库仓库')
                    return false;
                }
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['70%', '90%'],
                    title: "选择商品",
                    content: 'seach',
                })
            })
            Controller.api.bindevent();
        },
        seach:function(){
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/goods/index'+ location.search,
                    table: '',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                pageSize:15,
                // searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        // {checkbox: true,operate:false},
                        {field: 'id', title: 'ID', operate: false,class:'sid'},
                        {field: 'number', title: __('Goods_number'),sortable:true},
                        {field: 'title', title: __('title'),  operate: false,sortable:true},
                        {field: 'specs', title: __('specs')},
                        {field: 'stock', title: __('stock'),sortable:true},
                        {field: 'createtime', title: __('Create time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'returnorder',
                                    title: __('选中'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-fighter-jet',
                                    hidden:function(row){
                                        // if(row.status != '审核'){
                                        //     return true;
                                        // }
                                    },
                                    click: function(data){
                                        var sid =$(this).parents('tr').find('.sid').html()
                                        $(this).attr('style','display:none')
                                        parent.getrequisition(sid);
                                        // console.log(parent.getrequisition(sid))
                                        // console.log('返回结果:'+res)
                                    }}
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            $('table').on('dblclick','td',function(){
                var sid =$(this).parents('tr').find('.sid').html()
                parent.getrequisition(sid);
                var index = parent.Layer.getFrameIndex(window.name);
                parent.Layer.close(index);
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        seachbill: function () {
            $('.btn-success').click(function () {
                var turl = Config.this_url+'/goods/goods/dilllist?goods_id='+$('#goods_id').val()+'&storehouse_id='+$('#storehouse_id').val()+'&starttime='+$('#c-start_time').val()+'&endtime='+$('#c-end_time').val()+'&admin_id='+$('#c-admin_id').val();
                Backend.api.addtabs(turl, '库存状况明细', 'fa fa-circle-o fa-fw');
                var index = parent.Layer.getFrameIndex(window.name);
                parent.layer.close(index);
                return false;
            })
            Controller.api.bindevent();
        },
        lists:function(){
            Table.api.init({
                extend: {
                    index_url: 'goods/goods/lists' + location.search,
                    add_url: 'goods/goods/addgoodslog' + location.search,
                    table: 'goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),class:'gid'},
                        {field: 'title', title: __('Title'),operate: false},
                        {field: 'place_origin', title: __('Place_origin'),operate: false},
                        {field: 'unit', title: __('Unit'),operate: false},
                        {field: 'name', title: __('Name'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=1")},
                        {field: 'goods_num', title: __('Goods_num')},
                        {field: 'unit_price', title: __('unit_price'), operate: false},
                        {field: 'final_cost', title: __('Cost'), operate: false},
                        // {field: 'cost', title: __('Cost'), operate: false},
                        // {field: 'cost', title: __('Cost'), operate:'BETWEEN',operate: false},
                        // {field: 'specs', title: __('Specs')},
                        // {field: 'stock', title: __('Stock')},
                        // {field: 'cate.name', title: __('Cate.name')},
                        // {field: 'supplier.nickname', title: __('Supplier.nickname')},
                        {field: 'type', title: __('Type'), searchList: {1:__('正常采购'),2:__('库存填补')}},
                        {field: 'status', title: __('Status'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=12")},
                        {field: 'operate', title: __('Operate'), table: table, buttons: [
                                {
                                    name: 'examine',
                                    title: __('审核'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-random',
                                    hidden:function(value, row, index){
                                        if(value.ytype == 1 || value.ystatus > 1){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var gid =$(this).parents('tr').find('.gid').html()
                                        layer.confirm('是否通过？', {
                                            btn: ['确定','驳回'] //按钮
                                        }, function(){
                                            var url = Config.this_url+'/goods/goods/examine?ids='+gid
                                            $.post(url,{status:4},function (res) {
                                                layer.msg(res.msg)
                                                if(res.code == 1){
                                                    $('.btn-refresh').click()
                                                }
                                            })
                                        }, function(){
                                            var url = Config.this_url+'/goods/goods/examine?ids='+gid
                                            $.post(url,{status:5},function (res) {
                                                layer.msg(res.msg)
                                                if(res.code == 1){
                                                    $('.btn-refresh').click()
                                                }
                                            })
                                        });
                                    }},
                            ], events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        stocklist:function(){
            Table.api.init({
                extend: {
                    index_url: 'goods/goods/stocklist' + location.search,
                    table: 'goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: false,
                visible: false,
                showToggle: false,
                showColumns: false,
                search:false,
                showExport: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),class:'gid'},
                        {field: 'title', title: __('Title'),operate: false},
                        {field: 'number', title: __('编号'),operate: false},
                        {field: 'place_origin', title: __('Place_origin'),operate: false},
                        {field: 'unit', title: __('Unit'),operate: false},
                        {field: 'specs', title: __('规格'),operate: false},
                        {field: 'stock', title: __('Stock'),operate: false},
                        {field: 'weighting_cost', title: __('成本均价'),operate: false},
                        {field: 'balance', title: __('剩余金额'),operate: false},
                        {field: 'name', title: __('Name'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=1")},
                        {field: 'operate', title: __('Operate'), table: table, buttons: [
                                {
                                    name: 'examine',
                                    title: __('修改'),
                                    classname: 'btn btn-xs btn-error btn-click editstatus',
                                    icon: 'fa fa-eraser',
                                    hidden:function(value, row, index){

                                    },
                                    click: function(data){
                                        var gid = $(this).parents('tr').find('.gid').html()
                                        //prompt层
                                        layer.prompt({
                                            title: '设置库存! 除最初使用之外谨慎使用',
//                    area: ['800px', '350px'] //自定义文本域宽高
                                        }, function(value, index, elem){
                                            console.log(value)
                                            var data2=$('#zxr').val()
                                            console.log(data2)
                                            if(data2==="") {
                                                layer.tips("请填写成本均价", $('#zxr'));
                                                return;
                                            }
                                                var url = Config.this_url +'/goods/goods/updatestock'+location.search
                                               $.post(url,{hid:gid,pass:value,balance:data2},function (res) {
                                                   layer.close(index);
                                                   layer.msg(res.msg)
                                                   $('.btn-refresh').click()
                                               })

                                            layer.close(index);
                                        });
                                        $(".layui-layer-content").find('input').attr('placeholder','输入库存')
                                        $(".layui-layer-content").append("<br/><input type=\"text\" id=\"zxr\" class=\"layui-layer-input\" autocomplete=\"off\"  value=\"\" placeholder=\"请填写成本均价\"/>")
                                        $('.layui-layer-confirm').attr('style','display:none')
                                        // layer.prompt({title: '输入修改后库存，并确认',}, function(pass, index){
                                        //     var url = Config.this_url +'/goods/goods/updatestock'+location.search
                                        //    $.post(url,{hid:gid,pass:pass},function (res) {
                                        //        layer.close(index);
                                        //        layer.msg(res.msg)
                                        //        $('.btn-refresh').click()
                                        //    })
                                        // });
                                    }},
                                {
                                    name: 'examine',
                                    title: __('同步'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-exchange',
                                    hidden:function(value, row, index){

                                    },
                                    click: function(data){
                                        var gid = $(this).parents('tr').find('.gid').html()
                                        //prompt层
                                        layer.confirm('确定同步商城库存吗？', {
                                            btn: ['确定','再想想'] //按钮
                                        }, function(index){
                                            var url = Config.this_url +'/goods/goods/syncstock'+location.search
                                            $.post(url,{hid:gid},function (res) {
                                                layer.close(index);
                                                layer.msg(res.msg)
                                            })
                                        }, function(){

                                        });
                                    }},
                            ], events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                console.log(data);
                //这里我们手动设置底部的值
                $("#stock").text(data.stock);
                // $("#price").text(data.price);
                // $('#stock').click()
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        addgoodslog:function(){
            Form.api.bindevent($("form[role=form]"));
        },
        plists:function(){
            Table.api.init({
                extend: {
                    index_url: 'goods/goods/plists' + location.search,
                    add_url: 'goods/goods/addsalelog' + location.search,
                    table: 'goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),class:'gid'},
                        {field: 'title', title: __('Title'),operate: false},
                        {field: 'place_origin', title: __('Place_origin'),operate: false},
                        {field: 'unit', title: __('Unit'),operate: false},

                        {field: 'name', title: __('出库仓库'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=1")},
                        {field: 'goods_num', title: __('销售数量')},
                        {field: 'sale_price', title: __('销售价格'), operate:'BETWEEN'},
                        // {field: 'cost', title: __('Cost'), operate:'BETWEEN'},
                        // {field: 'specs', title: __('Specs')},
                        // {field: 'stock', title: __('Stock')},
                        // {field: 'cate.name', title: __('Cate.name')},
                        // {field: 'supplier.nickname', title: __('Supplier.nickname')},
                        {field: 'type', title: __('Type'), searchList: {1:__('正常销售'),2:__('库存填补')}},
                        {field: 'status', title: __('Status'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=13")},
                        {field: 'operate', title: __('Operate'), table: table, buttons: [
                                {
                                    name: 'examine',
                                    title: __('审核'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-random',
                                    hidden:function(value, row, index){
                                        if(value.ytype == 1 || value.ystatus > 1){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var gid =$(this).parents('tr').find('.gid').html()
                                        layer.confirm('是否通过？', {
                                            btn: ['确定','驳回'] //按钮
                                        }, function(){
                                            var url = Config.this_url+'/goods/goods/examines?ids='+gid
                                            $.post(url,{status:4},function (res) {
                                                layer.msg(res.msg)
                                                if(res.code == 1){
                                                    $('.btn-refresh').click()
                                                }
                                            })
                                        }, function(){
                                            var url = Config.this_url+'/goods/goods/examines?ids='+gid
                                            $.post(url,{status:5},function (res) {
                                                layer.msg(res.msg)
                                                if(res.code == 1){
                                                    $('.btn-refresh').click()
                                                }
                                            })
                                        });
                                    }},
                            ], events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        report: function () {
            console.log(Echarts.version);
            //这句话在多选项卡统计表时必须存在，否则会导致影响的图表宽度不正确
            $(document).on("click", ".charts-custom a[data-toggle=\"tab\"]", function () {
                var that = this;
                setTimeout(function () {
                    var id = $(that).attr("href");
                    var chart = Echarts.getInstanceByDom($(id)[0]);
                    chart.resize();
                }, 0);
            });

            // 基于准备好的dom，初始化echarts实例
            var lineChart = Echarts.init(document.getElementById('line-chart'), 'walden');

            // 指定图表的配置项和数据
            var option = {
                xAxis: {
                    type: 'category',
                    data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
                },
                yAxis: {
                    type: 'value'
                },
                series: [{
                    data: [49, 92, 61, 134, 90, 130, 120],
                    type: 'line'
                }]
            };

            // 使用刚指定的配置项和数据显示图表。
            lineChart.setOption(option);
            // 基于准备好的dom，初始化echarts实例
            var areaChart = Echarts.init(document.getElementById('area-chart'), 'walden');

            // 指定图表的配置项和数据
            var option = {
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
                },
                yAxis: {
                    type: 'value'
                },
                series: [{
                    data: [820, 932, 901, 934, 1290, 1330, 1320],
                    type: 'line',
                    areaStyle: {}
                }]
            };

            // 使用刚指定的配置项和数据显示图表。
            areaChart.setOption(option);

            var pieChart = Echarts.init(document.getElementById('pie-chart'), 'walden');
            var option = {
                tooltip: {
                    trigger: 'item',
                    formatter: '{a} <br/>{b}: {c} ({d}%)'
                },
                legend: {
                    orient: 'vertical',
                    left: 10,
                    data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
                },
                series: [
                    {
                        name: '访问来源',
                        type: 'pie',
                        radius: ['50%', '70%'],
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: false,
                                position: 'center'
                            },
                            emphasis: {
                                show: true,
                                textStyle: {
                                    fontSize: '30',
                                    fontWeight: 'bold'
                                }
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: [
                            {value: 335, name: '直接访问'},
                            {value: 310, name: '邮件营销'},
                            {value: 234, name: '联盟广告'},
                            {value: 135, name: '视频广告'},
                            {value: 1548, name: '搜索引擎'}
                        ]
                    }
                ]
            };
            // 使用刚指定的配置项和数据显示图表。
            pieChart.setOption(option);

            var barChart = Echarts.init(document.getElementById('bar-chart'), 'walden');
            option = {
                legend: {},
                tooltip: {},
                dataset: {
                    source: [
                        ['产品销售', '2015', '2016', '2017'],
                        ['风扇', 43.3, 85.8, 93.7],
                        ['电视机', 83.1, 73.4, 55.1],
                        ['空调', 86.4, 65.2, 82.5],
                        ['冰箱', 72.4, 53.9, 39.1]
                    ]
                },
                xAxis: {type: 'category'},
                yAxis: {},
                // Declare several bar series, each will be mapped
                // to a column of dataset.source by default.
                series: [
                    {type: 'bar'},
                    {type: 'bar'},
                    {type: 'bar'}
                ]
            };
            // 使用刚指定的配置项和数据显示图表。
            barChart.setOption(option);


            var barChart = Echarts.init(document.getElementById('simplebar-chart'));
            option = {
                xAxis: {
                    type: 'category',
                    axisLine: {
                        lineStyle: {
                            color: "#fff"
                        }
                    },
                    data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
                },
                yAxis: {
                    type: 'value',
                    axisLine: {
                        lineStyle: {
                            color: "#fff"
                        }
                    }
                },
                series: [{
                    data: [120, 200, 150, 80, 70, 110, 130],
                    type: 'bar',
                    itemStyle: {
                        color: "#fff",
                        opacity: 0.6
                    }
                }]
            };
            // 使用刚指定的配置项和数据显示图表。
            barChart.setOption(option);

            var barChart = Echarts.init(document.getElementById('smoothline-chart'));
            option = {
                textStyle: {
                    color: "#fff"
                },
                color: ['#fff'],
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
                    axisLine: {
                        lineStyle: {
                            color: "#fff"
                        }
                    }
                },
                yAxis: {
                    type: 'value',
                    splitLine: {
                        show: false
                    },
                    axisLine: {
                        lineStyle: {
                            color: "#fff"
                        }
                    }
                },
                series: [{
                    data: [820, 932, 901, 934, 1290, 1330, 1320],
                    type: 'line',
                    smooth: true,
                    areaStyle: {
                        opacity: 0.4
                    }

                }]
            };
            // 使用刚指定的配置项和数据显示图表。
            barChart.setOption(option);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="goods/goods/change" data-id="' + row.id + '"><i class="fa ' + (row.status == 'hidden' ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
                operate: function (value, row, index) {
                    return Template("operatetpl", {item: row, index: index});
                },

            },
        }
    };
    return Controller;
});